import service from "@/utils/request";
import { ElMessage } from "element-plus";

import { defaultIdea, type Idea, type IdeasResponse } from "@/types/idea";

function getIdeas(id: number, count: number) {
  return service({
    url: `/ideas?id=${id}&count=${count}`,
    method: "get",
  });
}

export async function apiGetIdeas(
  id: number,
  count: number
): Promise<IdeasResponse> {
  const rsp: any = await getIdeas(id, count);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data as IdeasResponse;
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "Get ideas failed",
      type: "error",
      center: true,
    });

    return {
      total_count: 0,
      idea_count: 0,
      ideas: [],
    };
  }
}

function getIdea(ideaId: number) {
  return service({
    url: `/idea?id=${ideaId}`,
    method: "get",
  });
}

export async function apiGetIdea(ideaId: number): Promise<Idea> {
  const rsp: any = await getIdea(ideaId);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data as Idea;
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "Get idea failed",
      type: "error",
      center: true,
    });

    return defaultIdea();
  }
}

function addIdea(data: Idea) {
  return service({
    url: "/idea",
    method: "post",
    data: data,
  });
}

export async function apiAddIdea(newIdea: Idea): Promise<number> {
  const rsp: any = await addIdea(newIdea);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data["id"];
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "Add idea failed",
      type: "error",
      center: true,
    });

    return 0;
  }
}

function viewInc(ideaId: number){
  return service({
    url: `/idea?id=${ideaId}/star-inc`,
    method: "get",
  });
}

export async function apiViewInc(ideaId: number): Promise<number> {
  const rsp: any = await viewInc(ideaId);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data["views"];
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "Blog view inc failed",
      type: "error",
      center: true,
    });

    return -1;
  }
}

function starInc(ideaId: number) {
  return service({
    url: `/idea?id=${ideaId}/star-inc`,
    method: "get",
  });
}

export async function apiStarInc(ideaId: number): Promise<number> {
  const rsp: any = await starInc(ideaId);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data["stars"];
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "Idea star inc failed",
      type: "error",
      center: true,
    });

    return -1;
  }
}

function starDec(ideaId: number) {
  return service({
    url: `/idea?id=${ideaId}/star-dec`,
    method: "get",
  });
}

export async function apiStarDec(ideaId: number): Promise<number> {
  const rsp: any = await starDec(ideaId);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data["stars"];
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "Idea star dec failed",
      type: "error",
      center: true,
    });

    return -1;
  }
}
